const { resolve } = require("path");
const express = require("express");
const app = express();
const session = require("express-session");
const sessionConfig = require("./config/session");
const api = require("./routes/api");
const db = require("./connect");

// 支持请求格式
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 设置静态文件目录
app.use("/static", express.static(resolve(__dirname, "static")));

// 设置session
app.use(session(sessionConfig));

// 前台路由
app.use("/api", api);

// 全局错误处理
app.use((err, req, res, next) => {
    if (err) {
        res.status(500).json({
            code: 500,
            msg: "Internal Server Error",
        });
        return;
    }
    next();
});

app.listen(3000, () => {
    console.log("Server started at port 3000");
});
